let stuInfo = [
    [{
        "stuID": "1",
        "stuName": "bdy",
        "stuAge": "18",
        "stuGender": "male",
        "stuScore": "100",
        "className": "1班"
    },
    {
        "stuID": "2",
        "stuName": "luc",
        "stuAge": "23",
        "stuGender": "female",
        "stuScore": "89",
        "className": "1班"
    },
    {
        "stuID": "3",
        "stuName": "lc",
        "stuAge": "17",
        "stuGender": "male",
        "stuScore": "93",
        "className": "1班"
    },
    {
        "stuID": "4",
        "stuName": "xu",
        "stuAge": "19",
        "stuGender": "female",
        "stuScore": "91",
        "className": "1班"
    },
    {
        "stuID": "5",
        "stuName": "chao",
        "stuAge": "20",
        "stuGender": "male",
        "stuScore": "95",
        "className": "1班"
    }
    ],
    undefined, //表示没有班级
    [{
        "stuID": "1",
        "stuName": "wf",
        "stuAge": "18",
        "stuGender": "female",
        "stuScore": "85",
        "className": "2班"
    },
    // {
    //     "stuID": "2",
    //     "stuName": "xtf",
    //     "stuAge": "19",
    //     "stuGender": "female",
    //     "stuScore": "91",
    //     "className": "2班"
    // },
    {
        "stuID": "3",
        "stuName": "xj",
        "stuAge": "19",
        "stuGender": "male",
        "stuScore": "92",
        "className": "2班"
    },
    {
        "stuID": "4",
        "stuName": "ls",
        "stuAge": "21",
        "stuGender": "male",
        "stuScore": "94",
        "className": "2班"
    },
    {
        "stuID": "5",
        "stuName": "xjs",
        "stuAge": "22",
        "stuGender": "female",
        "stuScore": "89",
        "className": "2班"
    }
    ],
    [{
        "stuID": "1",
        "stuName": "yaj",
        "stuAge": "22",
        "stuGender": "male",
        "stuScore": "86",
        "className": "3班"
    },
    {
        "stuID": "2",
        "stuName": "xj",
        "stuAge": "25",
        "stuGender": "female",
        "stuScore": "89",
        "className": "3班"
    },
    {
        "stuID": "3",
        "stuName": "xiz",
        "stuAge": "24",
        "stuGender": "female",
        "stuScore": "80",
        "className": "3班"
    },
    {
        "stuID": "4",
        "stuName": "yhq",
        "stuAge": "26",
        "stuGender": "female",
        "stuScore": "92",
        "className": "3班"
    },
    {
        "stuID": "5",
        "stuName": "npa",
        "stuAge": "19",
        "stuGender": "male",
        "stuScore": "96",
        "className": "3班"
    }
    ],
    [] //空数组表示有班级，单数没有没有学生
];
// 注意：删除班级用delete，删除学生用splice
// 2.管理员账号
let admin = {
    name: "qwe",
    passWord: "123"
}

let readline = require("readline-sync");

function showClassNumber() {
    let str = "";
    for (i = 0; i < stuInfo.length; i++) {
        if (stuInfo[i]) {
            str += `${i + 1}.第${i + 1}`
        }
    }
    str += `${stuInfo.length + 1}`
    return str;
}

function queryStudent(studentName) {
    let arr = [];
    for (i = 0; i < stuInfo.length; i++) {
        for (j = 0; j < stuInfo[i].length; j++) {
            if (stuInfo[i][j].stuName == studentName) {
                arr.push(stuInfo[i][j]);
            }
        }
    }
    return arr;
}

function queryStudent_number(classNumber, studentNumber) {
    for (i = 0; i < stuInfo[classNumber - 1].length; i++) {
        if (studentNumber == stuInfo[classNumber - 1][i].stuID) {
            return stuInfo[classNumber - 1][i];
        }
    }
}

function main() {
    let count = 3;
    while (count) {
        console.log("请输入用户名:")
        let userName = readline.question("");
        console.long("请输入密码:");
        let passWord = readline.question("");
        if (userName === admin.name && passWord === admin.passWord) {
            console.log("登录成功!");
            let mainFlag = true;
            while (mainFlag) {
                console.log("请选择功能: 1.查询 2.修改 3.增加 4.删除 5.转班 6.退出");
                let mainChoice = readline.question("") * 1;
                switch (mainChoice) {
                    case 1:
                        console.log("查询!");
                        let queryFlag = true;
                        while (queryFlag) {
                            console.log("请选择功能: 1.班级查询 2.学生查询 3.返回");
                            let queryChoice = readline.question("") * 1;
                            switch (queryChoice) {
                                case 1:
                                    while (true) {
                                        console.log("请选择要查询的班级: ", showClassNumber())
                                        let classNumber = readline.question("") * 1;
                                        if (stuInfo[classNumber - 1]) {
                                            console.table(stuInfo[classNumber - 1]);
                                        } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                            console.log("没有该班级!");
                                        }
                                        else if (classNumber == stuInfo.length + 1) {
                                            break;
                                        } else {
                                            console.log("输入有误! 请重新输入!")
                                        }
                                    }
                                    break;
                                case 2:
                                    console.log("请输入学生姓名: ")
                                    let studentName = readline.question("");
                                    let studentArr = queryStudent(studentName);
                                    if (studentArr.length) {
                                        console.log(studentArr);
                                    } else {
                                        console.log("没有找到该学生!");
                                    }
                                    break;
                                case 3:
                                    queryFlag = false;
                                    break;
                                default:
                                    console.log("输入有误! 请重新输入!");
                            }
                        }
                        break;
                    case 2:
                        console.log("修改!");
                        while (true) {
                            console.log("请选择要修改学生所在的班级: ", showClassNumber())
                            let classNumber = readline.question("") * 1;
                            if (stuInfo[classNumber - 1]) {
                                console.table(stuInfo[classNumber - 1]);
                                comsole.log("请输入要修改学生的学号:");
                                let studentNumber = readline.question("");
                                let studentObj = queryStudent_number(classNumber, studentNumber);
                                if (studentObj) {
                                    console.table([studentObj]);
                                    let changeFlag = true;
                                    while (true) {
                                        console.log("请选择要修改的内容: 1.姓名 2.性别 3.年龄 4.分数 5.返回");
                                        let changeChoice = readline.question("");
                                        switch (changeChoice) {
                                            case 1:
                                                let changeName=readline.question("");
                                                let nameReg=/^[a-2]{2,5}$/;
                                                if(nameReg.test(changeName)){
                                                    studentObj.stuName=changeName;
                                                    console.table([studentObj]);
                                                }else{
                                                    console.log("输入有误! 请重新输入!");
                                                }
                                                break;
                                            case 2:
                                                console.log("请输入修改后的性别: (male或者female)");
                                                let changeGender=readline.question("");
                                                let genderReg=/^male|female$/;
                                                if(nameReg.test(changeName)){
                                                    studentObj.stuName=changeName;
                                                    console.table([studentObj]);
                                                }else{
                                                    console.log("输入有误! 请重新输入!");
                                                }
                                            case 3:
                                                console.log("请输入修改后的年龄: (16-60)");
                                                let changeAge=readline.question("");
                                                let ageReg=/^[][]$/;
                                                if(nameReg.test(changeName)){
                                                    studentObj.stuName=changeName;
                                                    console.table([studentObj]);
                                                }else{
                                                    console.log("输入有误! 请重新输入!");
                                                }
                                                break;
                                            case 4:

                                                break;
                                            case 5:
                                                changeFlag = false;
                                                break;
                                            default:
                                                console.log("输入有误, 请重新输入!");

                                        }
                                    }
                                } else {
                                    console.log("没有该学生!");
                                }

                            } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                console.log("没有该班级!");
                            }
                            else if (classNumber == stuInfo.length + 1) {
                                break;
                            } else {
                                console.log("输入有误! 请重新输入!")
                            }
                        }
                        break;
                    case 3:
                        console.log("增加");

                        break;
                    case 4:
                        console.log("删除!");

                        break;
                    case 5:
                        console.log("转班!");

                        break;
                    case 6:
                        console.log("退出! 欢迎下次再来!");
                        mainFlag = false;
                        count = 0;
                        break;
                    default:
                        console.log("输入有误! 请重新输入!")
                }
            }
        }
    }
}
main();